﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class PhieuYeuCauBO : Csla.BusinessBase<PhieuYeuCauBO>
    {
        private Hashtable hstOldValue = new Hashtable();
        public Hashtable HstOldValue
        {
            get { return this.hstOldValue; }
            set { this.hstOldValue = value; }
        }

        #region Business Properties

        private int? _PhieuYeuCauID = null;
        public int? PhieuYeuCauID
        {
            get { return this._PhieuYeuCauID; }
            set { this._PhieuYeuCauID = value; PropertyHasChanged("PhieuYeuCauID"); }
        }

        private string _MaSo = "0";
        public string MaSo
        {
            get { return this._MaSo; }
            set { this._MaSo = value; PropertyHasChanged("MaSo"); }
        }

        private int? _KhachHangID = null;
        public int? KhachHangID
        {
            get { return this._KhachHangID; }
            set { this._KhachHangID = value; PropertyHasChanged("KhachHangID"); }
        }

        private DateTime? _NgayLapPhieu = null;
        public DateTime? NgayLapPhieu
        {
            get { return this._NgayLapPhieu; }
            set { this._NgayLapPhieu = value; PropertyHasChanged("NgayLapPhieu"); }
        }

        private string _NoiDung = null;
        public string NoiDung
        {
            get { return this._NoiDung; }
            set { this._NoiDung = value; PropertyHasChanged("NoiDung"); }
        }

        private string _GhiChu = null;
        public string GhiChu
        {
            get { return this._GhiChu; }
            set { this._GhiChu = value; PropertyHasChanged("GhiChu"); }
        }

        private double? _TongSo = null;
        public double? TongSo
        {
            get { return this._TongSo; }
            set { this._TongSo = value; PropertyHasChanged("TongSo"); }
        }

        private int? _ThucHien = null;
        public int? ThucHien
        {
            get { return this._ThucHien; }
            set { this._ThucHien = value; PropertyHasChanged("ThucHien"); }
        }

        private string _NguoiYeuCau = null;
        public string NguoiYeuCau
        {
            get { return this._NguoiYeuCau; }
            set { this._NguoiYeuCau = value; PropertyHasChanged("NguoiYeuCau"); }
        }

        private int? _NguoiLapPhieuID = null;
        public int? NguoiLapPhieuID
        {
            get { return this._NguoiLapPhieuID; }
            set { this._NguoiLapPhieuID = value; PropertyHasChanged("NguoiLapPhieuID"); }
        }

        private int? _NguoiThucHienID = null;
        public int? NguoiThucHienID
        {
            get { return this._NguoiThucHienID; }
            set { this._NguoiThucHienID = value; PropertyHasChanged("NguoiThucHienID"); }
        }

        private int? _USRC = Session.LoginID;
        public int? USRC
        {
            get { return this._USRC; }
            set { this._USRC = value; PropertyHasChanged("USRC"); }
        }

        private DateTime? _DATC = DateTime.MaxValue;
        public DateTime? DATC
        {
            get { return this._DATC; }
            set { this._DATC = value; PropertyHasChanged("DATC"); }
        }

        private int? _USRM = null;
        public int? USRM
        {
            get { return this._USRM; }
            set { this._USRM = value; PropertyHasChanged("USRM"); }
        }

        private DateTime? _DATM = null;
        public DateTime? DATM
        {
            get { return this._DATM; }
            set { this._DATM = value; PropertyHasChanged("DATM"); }
        }

        private bool? _DELE = false;
        public bool? DELE
        {
            get { return this._DELE; }
            set { this._DELE = value; PropertyHasChanged("DELE"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        protected override object GetIdValue()
        {
            return _PhieuYeuCauID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newPhieuYeuCauID = -1;
        private PhieuYeuCauBO()
        {
            _PhieuYeuCauID = newPhieuYeuCauID--;
        }

        public PhieuYeuCauBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public PhieuYeuCauBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static PhieuYeuCauBO NewRootPhieuYeuCauBO()
        {
            return DataPortal.Create<PhieuYeuCauBO>(new RootCriteria());
        }

        internal static PhieuYeuCauBO NewChildPhieuYeuCauBO()
        {
            return DataPortal.Create<PhieuYeuCauBO>(new ChildCriteria());
        }

        public static PhieuYeuCauBO GetRootPhieuYeuCauBO(int _PhieuYeuCauID)
        {
            return DataPortal.Fetch<PhieuYeuCauBO>(new RootCriteria(_PhieuYeuCauID));
        }

        public static PhieuYeuCauBO GetRootPhieuYeuCauBOByWhere(string where)
        {
            return DataPortal.Fetch<PhieuYeuCauBO>(where);
        }

        internal static PhieuYeuCauBO GetChildPhieuYeuCauBO(SqlDataReader dataReader)
        {
            return new PhieuYeuCauBO(dataReader);
        }

        public static void DeletePhieuYeuCauBO(int _PhieuYeuCauID)
        {
            DataPortal.Delete(new RootCriteria(_PhieuYeuCauID));
        }

        public static void DeletePhieuYeuCauBO(int _PhieuYeuCauID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_PhieuYeuCauID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int PhieuYeuCauID;
            public bool directDelete = false;
            public RootCriteria(int _PhieuYeuCauID)
            {
                this.PhieuYeuCauID = _PhieuYeuCauID;
                this.directDelete = false;
            }

            public RootCriteria(int _PhieuYeuCauID, bool directDelete)
            {
                this.PhieuYeuCauID = _PhieuYeuCauID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int PhieuYeuCauID;
            public bool directDelete = false;
            public ChildCriteria(int _PhieuYeuCauID)
            {
                this.PhieuYeuCauID = _PhieuYeuCauID;
                this.directDelete = false;
            }

            public ChildCriteria(int _PhieuYeuCauID, bool directDelete)
            {
                this.PhieuYeuCauID = _PhieuYeuCauID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("PhieuYeuCauBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[PhieuYeuCauID] = " + criteria.PhieuYeuCauID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("PhieuYeuCauBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["PhieuYeuCauID"] is DBNull))
                this._PhieuYeuCauID = (int)dataReader["PhieuYeuCauID"];
            if (!(dataReader["MaSo"] is DBNull))
                this._MaSo = (string)dataReader["MaSo"];
            if (!(dataReader["KhachHangID"] is DBNull))
                this._KhachHangID = (int)dataReader["KhachHangID"];
            if (!(dataReader["NgayLapPhieu"] is DBNull))
                this._NgayLapPhieu = (DateTime)dataReader["NgayLapPhieu"];
            if (!(dataReader["NoiDung"] is DBNull))
                this._NoiDung = (string)dataReader["NoiDung"];
            if (!(dataReader["GhiChu"] is DBNull))
                this._GhiChu = (string)dataReader["GhiChu"];
            if (!(dataReader["TongSo"] is DBNull))
                this._TongSo = (double)dataReader["TongSo"];
            if (!(dataReader["ThucHien"] is DBNull))
                this._ThucHien = (int)dataReader["ThucHien"];
            if (!(dataReader["NguoiYeuCau"] is DBNull))
                this._NguoiYeuCau = (string)dataReader["NguoiYeuCau"];
            if (!(dataReader["NguoiLapPhieuID"] is DBNull))
                this._NguoiLapPhieuID = (int)dataReader["NguoiLapPhieuID"];
            if (!(dataReader["NguoiThucHienID"] is DBNull))
                this._NguoiThucHienID = (int)dataReader["NguoiThucHienID"];
            if (!(dataReader["USRC"] is DBNull))
                this._USRC = (int)dataReader["USRC"];
            if (!(dataReader["DATC"] is DBNull))
                this._DATC = (DateTime)dataReader["DATC"];
            if (!(dataReader["USRM"] is DBNull))
                this._USRM = (int)dataReader["USRM"];
            if (!(dataReader["DATM"] is DBNull))
                this._DATM = (DateTime)dataReader["DATM"];
            if (!(dataReader["DELE"] is DBNull))
                this._DELE = (bool)dataReader["DELE"];

            GetOldData();
        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            this._USRC = Session.LoginID;
            this._DATC = DateTime.MaxValue;
            this._USRM = null;
            this._DATM = null;
            this._DELE = false;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("PhieuYeuCauBO_InsertBO");
            db.AddInParameter(cmd, "@MaSo", SqlDbType.NVarChar, this._MaSo);
            db.AddInParameter(cmd, "@KhachHangID", SqlDbType.Int, this._KhachHangID);
            db.AddInParameter(cmd, "@NgayLapPhieu", SqlDbType.DateTime, this._NgayLapPhieu);
            db.AddInParameter(cmd, "@NoiDung", SqlDbType.NVarChar, this._NoiDung);
            db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(cmd, "@TongSo", SqlDbType.Float, this._TongSo);
            db.AddInParameter(cmd, "@ThucHien", SqlDbType.Int, this._ThucHien);
            db.AddInParameter(cmd, "@NguoiYeuCau", SqlDbType.NVarChar, this._NguoiYeuCau);
            db.AddInParameter(cmd, "@NguoiLapPhieuID", SqlDbType.Int, this._NguoiLapPhieuID);
            db.AddInParameter(cmd, "@NguoiThucHienID", SqlDbType.Int, this._NguoiThucHienID);
            db.AddInParameter(cmd, "@USRC", SqlDbType.Int, this._USRC);
            db.AddOutParameter(cmd, "@NewPhieuYeuCauID", SqlDbType.Int, 10);
            db.AddOutParameter(cmd, "@NewDATC", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);
            this._PhieuYeuCauID = (int)cmd.Parameters["@NewPhieuYeuCauID"].Value;
            this._DATC = (DateTime)cmd.Parameters["@NewDATC"].Value;
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            this._USRM = Session.LoginID;
            this._DATM = DateTime.Now;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("PhieuYeuCauBO_UpdateBO");
            db.AddInParameter(cmd, "@PhieuYeuCauID", SqlDbType.Int, this._PhieuYeuCauID);
            db.AddInParameter(cmd, "@MaSo", SqlDbType.NVarChar, this._MaSo);
            db.AddInParameter(cmd, "@KhachHangID", SqlDbType.Int, this._KhachHangID);
            db.AddInParameter(cmd, "@NgayLapPhieu", SqlDbType.DateTime, this._NgayLapPhieu);
            db.AddInParameter(cmd, "@NoiDung", SqlDbType.NVarChar, this._NoiDung);
            db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(cmd, "@TongSo", SqlDbType.Float, this._TongSo);
            db.AddInParameter(cmd, "@ThucHien", SqlDbType.Int, this._ThucHien);
            db.AddInParameter(cmd, "@NguoiYeuCau", SqlDbType.NVarChar, this._NguoiYeuCau);
            db.AddInParameter(cmd, "@NguoiLapPhieuID", SqlDbType.Int, this._NguoiLapPhieuID);
            db.AddInParameter(cmd, "@NguoiThucHienID", SqlDbType.Int, this._NguoiThucHienID);
            db.AddInParameter(cmd, "@USRM", SqlDbType.Int, this._USRM);
            db.AddInParameter(cmd, "@DELE", SqlDbType.Bit, this._DELE);

            db.AddOutParameter(cmd, "@NewDATM", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);

            this._DATM = (DateTime)cmd.Parameters["@NewDATM"].Value;
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_PhieuYeuCauID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("PhieuYeuCauBO_DeleteBO");
            db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
            db.AddInParameter(cmd, "@PhieuYeuCauID", SqlDbType.Int, criteria.PhieuYeuCauID);
            db.ExecuteNonQuery(cmd);
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        private void GetOldData()
        {
            Type t = this.GetType();
            MemberInfo[] mi = t.GetMembers();
            int length = mi.Length;
            string name = "";
            object objValue = null;
            string strValue = null;

            for (int i = 0; i < length; i++)
                if (mi[i].MemberType == MemberTypes.Property)
                {
                    name = mi[i].Name;
                    if (StaticUtil.Log_ValidColumn(name))
                    {
                        objValue = t.GetProperty(name).GetValue(this, null);
                        if (objValue != null)
                            strValue = objValue.ToString();
                        else
                            strValue = "";

                        hstOldValue.Add(name, strValue);
                    }
                }
        }

        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_PhieuYeuCauID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
